// pages/order/order.js
const app=getApp();
Page({

  /*** 页面的初始数据*/
  data: {
    directory: app.globalData.directory,
    nav: [{ txt: '分类'}, { txt: '评价'}],
    current:0,
    foods:[],
    foodspec:{},
    condcrcurrent:0,
    numzong:0,
    priceszong:0,
    shopspec: true,
    goodindex:0,
    specac: [],
    specidac:[],
    foodid:'',//商品ID
    pj:[],
    // pjt: ['全部(320)', '追评(52)', '服务好(52)','快递好(56)','差评(10)'],
    pjindex:0,
    shopcar:[],
    addnum:true,
    inputNum: 1,
    thisPrice: 0,
    zthisPrice:0,
    foodid:0,
    goodid:0,
    moldid:0,
    specPrice: 0
  },
  navbind:function(e){
    var nav = this.data.nav;
    this.setData({
      current: e.currentTarget.id
    })
  },
  conswiper:function(e){
    var nav = this.data.nav;
    this.setData({
      current: e.detail.current 
    })
  },
  condcrtext:function(e){
    this.setData({
      condcrcurrent: e.currentTarget.dataset.id
    })
  },
  /*单纯加入购物车*/
  shopcaradd:function(e){
    var that=this;
    var shopcar = that.data.shopcar;
    var bol=true;
    // var foodid = that.data.foods[that.data.condcrcurrent].id;
    // console.log(foodid,11111)
    var goodid = that.data.goodid
    console.log(goodid,2222)
    if(shopcar.length==0){
      shopcar.push({
        // foodid: foodid,
        goodid: goodid,
        price: that.data.thisPrice,
        type:0,
        num: that.data.inputNum
      })
    }
    shopcar.forEach(function(item,index){
      if (item.goodid == goodid){
        item.num = that.data.inputNum;
          bol=false;
      }
      wx.showToast({
        title: '加入购物车成功',
        icon:'success'
      })
    })
    if(bol){
      shopcar.push({
        // foodid: foodid,
        goodid: goodid,
        price: that.data.thisPrice,
        type: 0,
        num: that.data.inputNum
      })
    }
    that.setData({
      shopcar:shopcar,
      shopspec: true,
      inputNum:1
    })
    this.znumprice();
    wx.setStorageSync('shopcar', that.data.shopcar);
    console.log(wx.getStorageSync('shopcar'),'现在的shopcar')
},
  /*格式中的加入购物车 */
  shopcaradd2: function (e) {
    var that = this;
    var shopcar = that.data.shopcar;
    var bol = true;
    // var foodid = that.data.foods[that.data.condcrcurrent].id;
    // var goodid = that.data.foods[that.data.condcrcurrent].goods[that.data.goodindex].id;
    var goodid = that.data.goodid
    if (shopcar.length == 0) {
      shopcar.push({
        // foodid: foodid,
        goodid: goodid,
        price: e.currentTarget.dataset.pri,
        type: 1,
        num: that.data.inputNum,
        specidac: that.data.specidac,
        specac: that.data.specac,
      })
    }
    shopcar.forEach(function (item, index) {
      if (item.goodid == goodid) {
        if (item.type == 1 && item.specidac.toString() == that.data.specidac.toString()) {
          item.num = that.data.inputNum;
          bol = false;
        }
      }
    })
    if (bol) {
      shopcar.push({
        // foodid: foodid,
        goodid: goodid,
        price: e.currentTarget.dataset.pri,
        type: 1,
        num: that.data.inputNum,
        specidac: that.data.specidac,
        specac: that.data.specac,
      })
    }
    that.setData({
      shopcar: shopcar,
      addnum: false,
      shopspec: true,
      inputNum: 1
    })
    this.znumprice();
    wx.setStorageSync('shopcar', that.data.shopcar);
  },
  /*显示格式*/
  shopspecshow: function (e) {
    console.log(e,'规格的显示格式')
    var moldid = e.currentTarget.dataset.modelid
    console.log(moldid)
    this.setData({
      moldid:moldid
    })
    var specidac = [];
    var specac = [];
    var that = this;
    var bol = true;
    // var foodid = that.data.foods[that.data.condcrcurrent].id;
    // var goodid = that.data.foods[that.data.condcrcurrent].goods[that.data.goodindex].id;
    var foodid = e.currentTarget.dataset.foodid
    var goodid = e.currentTarget.dataset.id
    that.data.foodid = e.currentTarget.dataset.id,
      wx.request({
        url: app.globalData.directory + 'api/v2/getGoodsSpec/' + e.currentTarget.dataset.id,
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token')
        },
        success: function (res) {
          console.log(res,'规格')
          if (res.data.status == 'ok') {
            var spec = res.data.data.model.spec;

            spec.forEach(function (items, index) {
              specac[index] = items.item[0].name,
                specidac[index] = items.item[0].id
            })

            that.data.shopcar.forEach(function (item, index) {
              if (item.foodid == foodid && item.goodid == goodid) {
                if (item.type == 1 && item.specidac.toString() == specidac.toString()) {
                  bol = false;
                  specac = item.specac;
                  specidac = item.specidac;
                }
              }
            })
            if (!bol) {
              that.setData({
                addnum: false
              })
            } else {
              that.setData({
                addnum: true
              })
            }
            that.setData({
              shopspec: false,
              foodid: that.data.foodid,
              goodid:goodid,
              goodindex: e.currentTarget.id,
              specac: specac,
              specidac: specidac,
              foodspec: res.data.data,
              'foodspec.tit': e.currentTarget.dataset.tit,
               specPrice: e.currentTarget.dataset.price,
            })

          } else {
            wx.showToast({
              title: '请求失败,请稍候',
              icon: 'none',
              duration: 2000,
            })
          }
        }
      })
  },

  /*格式中价格的变化*/
  speccon: function (e) {
    var specidacstr = '';
    var that = this;
    var bol = true;
    var foodid = that.data.foods[that.data.condcrcurrent].id;
    var goodid = that.data.foods[that.data.condcrcurrent].goods[that.data.goodindex].id;
    this.data.specac[e.currentTarget.dataset.id] = e.currentTarget.dataset.text;
    this.data.specidac[e.currentTarget.dataset.id] = e.currentTarget.dataset.current;
    this.setData({
      specac: that.data.specac,
      specidac: that.data.specidac
    })
    that.data.shopcar.forEach(function (item, index) {
      if (item.foodid == foodid && item.goodid == goodid) {
        if (item.type == 1 && item.specidac.toString() == that.data.specidac.toString()) {
          bol = false;
        }
      }
    })
    if (!bol) {
      that.setData({
        addnum: false
      })
    } else {
      that.setData({
        addnum: true
      })
    }
    that.data.specidac.forEach(function (item, index) {
      specidacstr += '_' + item;
    })
    wx.request({
      url: app.globalData.directory + 'api/v2/getGoodsSpecPrice/',
      data: {
        'id': that.data.foodid,
        'spec_ids': specidacstr,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        var inputNum = that.data.inputNum
        var specPriceA = inputNum * (res.data.data)
        console.log(specPriceA, '*////*/*/**/*')
        var specPrice = that.data.specPrice
        specPrice = res.data.data
        if (res.data.status == 'ok') {
          that.setData({
            'foodspec.price.price': res.data.data,
            specPrice: specPrice,
            specPriceA: specPriceA
          })

        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  // 加
  addCount: function (e) {
    var inputNum = this.data.inputNum
    inputNum = inputNum + 1
    var zthisPrice = inputNum * this.data.thisPrice
    var specPriceA = inputNum * this.data.specPrice
    this.setData({
      inputNum: inputNum,
      zthisPrice:zthisPrice,
      specPriceA: specPriceA
    })
  },
  //减
  downCount: function (e) {
    var inputNum = this.data.inputNum
    if (inputNum < 2) {
      return false
    }
    inputNum = inputNum - 1
    var zthisPrice = inputNum * this.data.thisPrice
    var specPriceA = inputNum * this.data.specPrice
    this.setData({
      inputNum: inputNum,
      zthisPrice:zthisPrice,
      specPriceA: specPriceA
    })
  },
  //显示隐藏模态框
  shopspechide:function(e){
    this.setData({
      shopspec:true
    })
  },
  // input事件
  Input: function (e) {
    console.log(e)
    var inputNum = parseInt(e.detail.value);
    if (inputNum < 1) {
      inputNum = 1
    }
    if (inputNum > 999) {
      inputNum = 999
    }
   
    var zthisPrice = inputNum * this.data.thisPrice
    var specPriceA = inputNum * this.data.specPrice
    this.setData({
      inputNum: inputNum,
      zthisPrice:zthisPrice,
      specPriceA: specPriceA
    })
  },
  //我的显示格式
  shopspecShow: function (e) {
    var moldid = e.currentTarget.dataset.modelid
    console.log(moldid)
    this.setData({
      moldid: moldid
    })
    this.setData({
      shopspec: false,

    })
    var that = this;
    console.log(e)
    var foodid = e.currentTarget.dataset.foodid
    var goodid = e.currentTarget.dataset.id
    var inputNum = that.data.inputNum
    var zthisPrice = inputNum * this.data.thisPrice
    that.setData({
      'foodspec.tit': e.currentTarget.dataset.tit,
      thisPrice: e.currentTarget.dataset.price,
      zthisPrice: zthisPrice,
      foodid:foodid,
      goodid:goodid
    })
  },
  //
  shopspechidel:function(e){},
  pjt:function(e){
    this.setData({
      pjindex:e.currentTarget.id
    })
  },
  /*总的价格数量 */
  /*总的价格数量 */
  znumprice: function () {
    var znum = 0;
    var zprice = 0;
    if (!this.data.shopcar) return false;
    this.data.shopcar.forEach(function (item, index) {
      znum += item.num
      zprice += item.price * item.num
    })
    if (znum > 99) {
      znum = '99+'
    }
    zprice = parseInt(zprice * 100) / 100;
    this.setData({
      numzong: znum,
      priceszong: zprice,
    })
    wx.setStorageSync('priceszong', this.data.priceszong);
  },
  /*预览照片 */
  previewImage:function(e){
    console.log(e.currentTarget.dataset.img, e.currentTarget.dataset.imgs);
    var imgs = e.currentTarget.dataset.imgs;
    var imgs1 = [];
    imgs.forEach(function(item,index){
      imgs1.push(item.img)
    })
    wx.previewImage({
      current:e.currentTarget.dataset.img,
      urls:imgs1
    })
  },
  /*** 生命周期函数--监听页面加载*/
  
  onLoad: function (options) {
    var that=this;
    wx.request({
      url: app.globalData.directory +'api/v2/getGoodsAll',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res,'分类所有商品')
        if(res.data.status=='ok'){
          that.setData({
            foods:res.data.data
          })
        }else{
          wx.showToast({
            title: '请求失败,请重试',
            icon:'none',
            duration:2000,
          })
        }
      }
    })
    /*评论*/
    wx.request({
      url: app.globalData.directory + 'api/v2/comment/getlist',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            pj: res.data.data
          })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
    // if (wx.getStorageSync('shopcar').length == 0) {
    if (typeof (wx.getStorageSync('shopcar')) !== 'string') {
      this.setData({
        shopcar: wx.getStorageSync('shopcar')
      })
    }
    // }
    this.znumprice();
  },

  /*** 生命周期函数--监听页面初次渲染完成*/
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if(typeof(wx.getStorageSync('shopcar')) !== 'string'){
      this.setData({
        shopcar: wx.getStorageSync('shopcar')
      })
    }
    this.znumprice();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})